﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace sfsocket.corelib.message
{
    public class RawMessage
    {
        public int Length;
        public int MessageType;
        public byte[] Buffer;

        public RawMessage()
        { }

        public RawMessage(int _msgtype, byte[] _buf)
        {
            Length = 4 + _buf.Length;
            MessageType = _msgtype;
            Buffer = _buf;
        }

        public byte[] GetBytes()
        {
            byte[] buff = new byte[Length + 4];
            Stream memStream = new MemoryStream(buff);
            BinaryWriter writer = new BinaryWriter(memStream);
            writer.Write(IPAddress.HostToNetworkOrder(Length));
            writer.Write(IPAddress.HostToNetworkOrder(MessageType));
            writer.Write(Buffer);
            writer.Close();
            memStream.Close();
            return buff;
        }

        public void Dump()
        { }

        public void DumpHex()
        {
            Console.WriteLine("Hex Dump:");
            foreach (byte b in GetBytes())
            {
                Console.Write("0x{0:X}, ", b);
            }
        }
    }
}
